package com.wlyuan.open.scanner.repository.impl;

import com.wlyuan.open.scanner.dao.entity.CompanyDO;
import com.wlyuan.open.scanner.dao.mapper.CompanyMapper;
import com.wlyuan.open.scanner.domain.tenant.CompanyTenant;
import com.wlyuan.open.scanner.domain.tenant.DriverTenant;
import com.wlyuan.open.scanner.domain.tenant.TenantConvert;
import com.wlyuan.open.scanner.repository.CompanyRepository;
import com.wlyuan.open.scanner.repository.PostPoneCodeRepository;
import com.wlyuan.open.scanner.repository.ShareCompanyRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import lombok.var;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;
import java.util.Optional;

/**
 * @author lijing
 * @description
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class CompanyRepositoryImpl implements CompanyRepository {
    private final CompanyMapper companyMapper;
    private final ShareCompanyRepository shareCompanyRepository;
    private final PostPoneCodeRepository postPoneCodeRepository;

    @Override
    public List<CompanyTenant> companyTenantList() {
        logger.info("名称不能为空，统一社会编码不能为空,clientCode is not null,company_name is not null,Company_Status = 2 and invalid = 0");
        List<CompanyDO> list = companyMapper.companyTenantList();
        if (CollectionUtils.isEmpty(list)) {
            return Collections.emptyList();
        }
        logger.info("老数据数据量，size：{}", list.size());
        list.forEach(c -> {
            process(c);
        });
        return TenantConvert.convert(list);
    }

    @Override
    public List<DriverTenant> driverTenantList() {
        logger.info("名称和手机号不能为空, clientCode is not null,company_name is not null,Company_Status = 2 and invalid = 0");
        List<CompanyDO> list = companyMapper.driverTenantList();
        if (CollectionUtils.isEmpty(list)) {
            return Collections.emptyList();
        }

        logger.info("老数据数据量，size：{}", list.size());
        list.forEach(c -> {
            process(c);
        });
        return TenantConvert.convertDriverList(list);
    }

    @Override
    public CompanyTenant getById(Long companyId) {
        return Optional.ofNullable(companyId).map(id -> {
            var company = companyMapper.selectById(id);
            process(company);
            return TenantConvert.convert(company);
        }).orElse(null);
    }

    @Override
    public List<Long> getByTime(String startTime, String endTime) {
        return companyMapper.getByTime(startTime, endTime);
    }

    @Override
    public DriverTenant getDriverTenantById(Long companyId) {
        return Optional.ofNullable(companyId).map(id -> {
            var driver = companyMapper.getDriverTenantById(id);
            return Optional.ofNullable(driver).map(tenant -> {
                //邀请者信息
                var share = shareCompanyRepository.getInviterByCompanyId(id);
                Optional.ofNullable(share).ifPresent(s -> {
                    tenant.setInviterTenantId(s.getInviterTenantId());
                    tenant.setInviterUserId(s.getInviterUserId());
                    tenant.setInviterMobile(s.getInviterMobile());
                    tenant.setInviterName(s.getInviterName());
                });
                process(tenant);
                return TenantConvert.convertDriver(tenant);
            }).orElse(null);

        }).orElse(null);
    }

    @Override
    public CompanyTenant getCompanyTenantById(Long companyId) {
        return Optional.ofNullable(companyId).map(id -> {
            var company = companyMapper.getCompanyTenantById(id);
            return Optional.ofNullable(company).map(tenant -> {
                //邀请者信息
                var share = shareCompanyRepository.getInviterByCompanyId(id);
                Optional.ofNullable(share).ifPresent(s -> {
                    tenant.setInviterTenantId(s.getInviterTenantId());
                    tenant.setInviterUserId(s.getInviterUserId());
                    tenant.setInviterMobile(s.getInviterMobile());
                    tenant.setInviterName(s.getInviterName());
                });
                process(tenant);
                return TenantConvert.convert(tenant);
            }).orElse(null);

        }).orElse(null);
    }

    private void process(CompanyDO companyDO) {
        postPoneCodeRepository.tenantConfig(companyDO);
    }
}
